(function () {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:fuwuList
     * @description
     * # fuwuList 
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function ($templateCache, dataService) {
                var tplName = "views/directives.useprocess.html";
                dataService.tpl(tplName).then(function (html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('useProcess', function () {
            return {
                restrict: 'E',
                templateUrl: "views/directives.useprocess.html",
                // template: "<a href='javascript:void(0);'   ng-click='showPop();'>{{'装修风格' | translate}}</a> ",
                scope: {

                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', 'ERP_CONFIG', '$location', 'cacheService',

                    function ($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, ERP_CONFIG, $location, cacheService) {

                        function getImage() {
                            var scence = "ESTO" + "0";
                            console.error("scence", scence);
                            dataService.acodeunlimitGet(ERP_CONFIG.BackAppId, scence, true, "pages/admin/index/index").then(function (r) {
                                $scope.dataimage = "data:image/png;base64," + r;
                            });
                        }
                        //查询商品信息
                        $scope.getItems = function () {
                            $scope.selectStore = 0;
                            var query = { query: { store_id: $rootScope.store_id } };
                            dataService.itemGet(query).then(function (list) {
                                console.error("查询商品信息", list);
                                if (list && list.items.length) {
                                    $scope.itemslist = list.items;
                                    console.error("2==商品信息", $scope.itemslist);
                                } else {
                                    $scope.itemslist = 0;
                                }
                            });
                        }

                        //选择店铺类型
                        $scope.checkStore = function (id) {
                            if (id) {
                                $scope.selectStore = id;
                            }
                        }
                        //店铺查询
                        function storeGet() {
                            if ($rootScope.store_id) {
                                return dataService.storeOne($rootScope.store_id).then(function (result) {
                                    console.error("店铺信息", result);
                                    // result.store_id = 0;
                                    if (result && result.store_id) {
                                        $scope.store = result;
                                        console.error("1==店铺信息", $scope.store);
                                        $scope.checking = true;
                                        return $scope.store;
                                    } else {
                                        if (!$scope.checking) {
                                            $timeout(storeGet, 1000);
                                        }
                                        return dataService.resolve();
                                    }
                                });
                            } else {
                                return dataService.resolve();
                            }
                        }
                        //复制数据
                        $scope.storeCpay = function () {
                            if ($scope.selectStore) {
                                var query = {
                                    old_store_id: $scope.selectStore,
                                    new_store_id: $rootScope.store_id,
                                };
                                console.error("执行复制店铺数据", $scope.query, $scope.selectStore);
                                dataService.copyStoreData(query).then(function (result) {
                                    if (result.Msg) {
                                        helper.message(result.Msg, 'error');
                                    } else {
                                        $timeout(function () {
                                            $scope.getItems();
                                            $scope.authrizeAppGet();
                                        }, 500);
                                    }
                                });
                            } else {
                                //没有找到数据进行查询
                                $scope.authrizeAppGet();
                            }
                        }

                        //授权成功回调
                        $scope.authorizationCallback = function (data) {
                            if (data) {
                                $scope.authStore = data;
                            }
                        }
                        //授权小程序
                        $scope.addStoreApp = function () {
                            if ($scope.authStore) {
                                var Adddata = {
                                    content: {
                                        app_id: $scope.authStore.app_id,
                                        store_id: $rootScope.store_id,
                                        partner_id: $scope.authStore.partner_id,
                                    }
                                };
                                return dataService.storeAppAdd(Adddata).then(function (retu) {
                                    if (retu.store_apps_id) {
                                        $scope.authStore = 0;
                                        $scope.authrizeAppGet();
                                        $scope.getLayout();
                                    } else {
                                        helper.message(retu.Msg, "error");
                                    }
                                });
                            } else {
                                $scope.authrizeAppGet();
                                $scope.getLayout();
                            }

                        }

                        //回调方法 进行保存选择的装修样式
                        $scope.layoutCallback = function (data) {
                            console.error("data", data);
                            if (data) {
                                $scope.Publics = clone(data);
                            }
                        }
                        //店铺装修
                        $scope.submitForm = function ($event) {
                            if ($scope.Publics) {
                                if (!$scope.oldStore_id) {
                                    helper.myAlert("请选择需要复制的小程序！");
                                    return false;
                                }
                                // helper.myAlert("请勿操作，小程序装修中！");
                                var colordata = clone($scope.Publics);
                                console.error("$scope.Publics", colordata, $scope.tmpid, $scope.wxappid);
                                //执行复制 \
                                $scope.old_wxappid = 'wx04607907184215ec';
                                if ($scope.oldStore_id == '4290') {
                                    $scope.old_wxappid = 'wxf90793e69364fde2';
                                }
                                var query = { new_store_id: $rootScope.store_id, old_store_id: $scope.oldStore_id, new_wxappid: $scope.wxappid, old_wxappid: $scope.old_wxappid, balckcolor: colordata, template_id: $scope.tmpid.template_id };
                                dataService.copyLayoutData(query).then(function (result) {
                                    if (result.return) {
                                        // helper.myAlert("设置成功！");
                                        if ($scope.wxappid == 'wx04607907184215ec') {
                                            $scope.initData().then($scope.show);
                                            $scope.getLayout();
                                        } else {
                                            $scope.testQrCode();
                                            $scope.getLayout();
                                        }
                                        $scope.Publics = 0;
                                    } else {
                                        helper.myAlert("error" + result.Msg);
                                    }
                                });
                            } else {
                                if (!$scope.Publics) {
                                    helper.myAlert("请选择装修色系！");
                                    return false;
                                }
                            }
                        }

                        $scope.initData = function () {
                            var scence = "S" + $rootScope.store_id;
                            $scope.path = "pages/index/index";
                            return dataService.acodeunlimitGet($scope.wxappid, scence, true, $scope.path).then(function (qrimage) {
                                if (qrimage) {
                                    return $scope.qrimage = "data:image/png;base64," + qrimage;
                                } else {
                                    return $scope.qrimage = "";
                                }
                            });
                        }

                        $scope.show = function () {
                            $.alert({
                                backdrop: 'static',
                                keyboard: true,
                                title: '<span>小程序二维码，请使用微信扫码查看效果</span>',
                                body: '<div><div class="sui-form sui-form"><form class="sui-form" style="margin:0px;"><div class="sui-form form-horizontal" style="text-align: center;"><img ng-src="{{qrimage}}" width="240"><div ng-show="itempath && qrimage">商品地址: <input ng-model="itempath" type="text" class="input-xlarge" disabled="disabled" ></div></div></div>',
                                // okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                hasfoot: false,
                                transition: true,
                                width: 'small',
                                // height: '200',
                                show: function (e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function (e) {

                                },
                            });
                        }
                        //获取体验二维码
                        $scope.testQrCode = function () {
                            dataService.isvCodeQrcodeGet($scope.wxappid).then(function (qrimage) {
                                $scope.qrimage = "data:image/png;base64," + qrimage;
                                var qrcodeWin = $.alert({
                                    backdrop: "static",
                                    keyboard: false,
                                    title: '{{"请用微信扫描体验"|translate}}',
                                    body: '<p style="text-align:center"><img ng-src="{{qrimage}}" width="240" /></p>',
                                    okBtn: '{{"关闭"|translate}}',
                                    closeBtn: true,
                                    hasfoot: false,
                                    transition: true,
                                    width: 'small',
                                    show: function (e) {
                                        $compile($(e.currentTarget).contents())($scope);
                                    },
                                    okHide: function (e) { },
                                    shown: function (e) { }
                                });
                            });
                        }

                        //装修店铺
                        function layoutGet() {
                            var data = {
                                appid: $scope.authrizeApp.app_id,
                                store_id: $rootScope.store_id,
                                name: "UI-MOd-home",
                            }
                            return dataService.wxappOne(data).then(function (list) {
                                if (list && list.value) {
                                    $scope.layout = list.value;
                                    return $scope.layout;
                                } else {
                                    return;
                                }
                            });
                        }

                        //读取授权app信息
                        $scope.authrizeAppGet = function (type) {
                            // return $timeout(function() {
                            return dataService.sellerAuthorizeAuthorizer_infoGet($rootScope.store_id, null, 1).then(function (results) {
                                if (results && results[0]) {
                                    var result = [];
                                    _.map(results, function (da, ke) {
                                        if (da.service_type_id == '2') {
                                            result.push(da);
                                        }
                                    });
                                    $scope.authrizeApp = result[0];
                                    $scope.wxappid = $scope.authrizeApp.app_id;
                                    if (type) {
                                        if ($scope.wxappid == 'wx04607907184215ec') {
                                            $scope.Current = 0;
                                        } else {
                                            if ($scope.authrizeApp.template_id > 0) {
                                                $scope.Current = $scope.authrizeApp.template_id;
                                            } else {
                                                $scope.Current = 0;
                                            }
                                        }
                                        return $scope.Current;
                                    }
                                    console.error("查询授权的小程序", $scope.wxappid);
                                } else {
                                    $scope.Current = 0;
                                    return $scope.Current;
                                }
                            });
                            // });
                        }

                        //查询装修
                        $scope.getLayout = function () {
                            $scope.layoutone = 0;
                            if ($scope.wxappid) {
                                var query = {
                                    store_id: $rootScope.store_id,
                                    app_id: $scope.wxappid,
                                    name: "UI-MOd-home",
                                    types: "ui"
                                };
                                dataService.wxappGet({ query: query }).then(function (list) {
                                    console.error("查询装修数据", list.wxappcfg);
                                    if (list.wxappcfg.length) {
                                        $scope.layoutone = list.wxappcfg;
                                        console.error("4==装修数据", $scope.layoutone);
                                    }
                                });
                            }
                        }

                        function init() {
                            $scope.store_id = $rootScope.store_id;
                            $scope.s_partner_id = $rootScope.s_partner_id;
                            $scope.authorlist = [
                                { "store_name": "珠宝", "store_id": "4347" },
                                { "store_name": "美甲美睫", "store_id": "4489" },
                                { "store_name": "美容美发", "store_id": "4492" },
                                { "store_name": "婚纱摄影", "store_id": "4496" },
                                { "store_name": "酒店类型", "store_id": "4147" },
                                { "store_name": "官网版", "store_id": "4385" },
                                { "store_name": "个人版", "store_id": "4611" },
                                { "store_name": "教育", "store_id": "4711" },
                            ];
                            if ($scope.s_partner_id == 1) {
                                $scope.authorlist.push({ "store_name": "特殊店铺", "store_id": "4290" });
                            }
                            $scope.authrizeAppGet(1).then(function (data) {
                                if (!data) {
                                    console.error("$scope.Current", $scope.Current);
                                    getInfo();
                                }
                            });
                        }

                        function getInfo() {
                            storeGet().then(function (data) {
                                if (!data) {
                                    getImage();
                                }
                            });
                            //查询商品信息
                            $scope.getItems();
                            $scope.getLayout();
                        }



                        init();
                        $scope.$on('event:up-store-id', function () {
                            init();
                        });

                    }
                ]
            }
        })
        .directive('getStoreStatus', function () {
            return {
                restrict: 'E',
                // templateUrl: "views/directives.useprocess.html",
                template: "<a href='javascript:void(0);'   ng-click='init();'>{{title | translate}}</a> ",
                scope: {
                    title: '@',
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', 'ERP_CONFIG', '$location', 'cacheService',
                    function ($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, ERP_CONFIG, $location, cacheService) {
                        //如果没有创建 弹出二维码 然后进行轮训查询 
                        $scope.init = function () {
                            $scope.checking = true;
                            getImage().then($scope.showPop);
                        }

                        function getImage() {
                            var scence = "ESTO" + "0";
                            console.error("scence", scence);
                            return dataService.acodeunlimitGet(ERP_CONFIG.BackAppId, scence, true, "pages/admin/index/index").then(function (r) {
                                return $scope.dataimage = "data:image/png;base64," + r;
                            });
                        }


                        $scope.showPop = function () {
                            $.alert({
                                backdrop: 'static',
                                keyboard: true,
                                title: '{{"扫描创建门店" | translate}}',
                                body: '<div class="sui-form form-horizontal" style="text-align: center;"><img ng-src="{{dataimage}}" width="250" style="border: 2px solid;" /><br />微信扫描上面的二维码操作</div>',
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                hasfoot: false,
                                transition: true,
                                width: 'small',
                                show: function (e) {
                                    if ($scope.dataimage) {
                                        getStore();
                                    }
                                    $compile($(e.currentTarget).contents())($scope);
                                },
                                okHide: function (e) {
                                    $scope.checking = false;
                                },
                                hide: function (e) {
                                    //手动关闭 弹窗 
                                    $scope.checking = false;
                                    // rmAlert();
                                    console.error("hidehidehide");
                                },
                            });
                        }

                        function getStore() {
                            var query = {
                                page_size: 1
                            };
                            dataService.storeGet(query).then(function (redata) {
                                if (redata.store) {
                                    //查询到 店铺信息进行 弹窗选择
                                    //
                                    // rmAlert();
                                } else {
                                    if ($scope.checking) {
                                        $timeout(getStore, 1000);
                                    }
                                }
                            });
                        }
                    }
                ]
            }
        })
        .run(["$templateCache", "dataService",
            function ($templateCache, dataService) {
                var tplName = "views/directives.useroperatiosteps.html";
                dataService.tpl(tplName).then(function (html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('userOperatioSteps', function () {
            return {
                restrict: 'E',
                templateUrl: "views/directives.useroperatiosteps.html",
                scope: {
                    user: "="
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', 'ERP_CONFIG', '$location', 'cacheService',
                    function ($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, ERP_CONFIG, $location, cacheService) {
                        $scope.init = function () {
                            dataService.getUserSteps({ user_id: $scope.user }).then(function (result) {
                                if (result.steps) {
                                    $scope.steps = result.steps;
                                } else {
                                    $scope.steps = 0;
                                }
                            });
                        }

                        $scope.init();
                    }]
            }
        })
        .directive('subscribe', function () {
            return {
                restrict: 'E',
                template: "<span><span ng-show='is_subscribe'><div class='sui-msg msg-success'><div class='msg-con'>关注公众号</div><s class='msg-icon'></s></div></span><span ng-show='!is_subscribe'><div class='sui-msg msg-error'><div class='msg-con'>未关注公众号</div><s class='msg-icon'></s></div></span></span>",
                scope: {
                    unionid: "="
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', 'ERP_CONFIG', '$location', 'cacheService',
                    function ($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper, ERP_CONFIG, $location, cacheService) {
                        $scope.init = function () {
                            var data = {
                                "unionid": $scope.unionid,
                            };
                            dataService.phpsubscribeGet(data).then(function (re) {
                                if (re.is_subscribe) {
                                    $scope.is_subscribe = re.is_subscribe;
                                }
                            });
                        }
                        $scope.init();
                    }]
            }
        })
})();